const http=require("http");
const fs =require("fs");
const path=require("path")
const {URL}=require("url")
http.createServer((req,res)=>{
    switch(req.method){
        case "GRT":
            getResource(req,res);
            break;
        case "POST":
            postData(req,res);
            break;
        case "DELETE":
            deleteData(req,res);
            break;
        case "PUT":
            updataData(req,res);
            break;
    }

}).listen(3000)

function getResource(req,res){
    let urlObj=new URL(req.url,"http://192.168.174.166:3000");
    let pageName="";
    let id=urlObj.searchParams.get("id")
    switch(urlObj.pathname){
        case "/":
            pageName="login.html";
            break;
        case "/list":
            pageName="list.html";
            break;
        case "/detail":
            res.end("this is detail page"+id);
            return;
        default:
            pageName="404.html";
            break;
    }
    let filePath=path.join(__dirname,pageName);
    let fileContent=fs.readFileSync(filePath);
    res.end(fileContent)
}

function postData(req,res){
    res.end("this is a post request")
}

console.log("server is listening 3000")